package com.zzyy.study.config;

import com.github.benmanes.caffeine.cache.Cache;
import io.lettuce.core.RedisClient;
import io.lettuce.core.TrackingArgs;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.support.caching.CacheFrontend;
import io.lettuce.core.support.caching.ClientSideCaching;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @auther zzyy
 * @create 2024-01-24 17:50
 * localCacheCaffeine
 */
@Slf4j
@NoArgsConstructor
public class CacheFrontendContext
{
    @Getter
    private CacheFrontend cacheFrontend;

    private RedisClient redisClient;
    private Cache localCacheCaffeine;

    StatefulRedisConnection<String,String> connection;

    public CacheFrontendContext(RedisClient redisClient, Cache localCacheCaffeine)
    {
        this.redisClient = redisClient;
        this.localCacheCaffeine = localCacheCaffeine;
    }

    public void check()
    {
        if (connection != null)
        {
            if(connection.isOpen())
            {
                System.out.println("-------redis 链接都正常");
                return;
            }
        }

        try
        {
            connection = redisClient.connect();

            cacheFrontend = ClientSideCaching.enable(new CaffeineCacheAccessor(localCacheCaffeine),
                    connection, TrackingArgs.Builder.enabled());

            connection.addListener(message -> {
                List<Object> content = message.getContent(StringCodec.UTF8::decodeKey);
                log.info("-----监听到远程redis有变化，属性type:{},connect:{}",message.getType(),content);
                //invalidate 表示原本的localCacheCaffeine内容失效了，重置
                if(message.getType().equalsIgnoreCase("invalidate"))
                {
                    List<String> keys = (List<String>)content.get(1);
                    keys.forEach(key -> {
                        localCacheCaffeine.invalidate(key);
                    });
                }
            });
            log.warn("The redis client connect is success且监听器connection.addListener有效");
        }catch (Exception e){
            e.printStackTrace();
            log.error("The redis client disconnected,bad luck+Fuck," +
                    "reconnecting...please wait a moment"+"\t"+e.getMessage());
        }
    }
}
